<?php
	/**
	 * 
	 */
	class sanpham_controller extends CI_Controller {
		
		function __construct() {
			parent::__construct();
			$this->load->helper('url');
            $this->load->model('taikhoan_model');
            $this->load->model('sanpham_model');
            $this->load->database();
            session_start();
		}
		
		function index(){
            $data['items'] = $this->sanpham_model->layloaisanpham();
            $data['menu'] = $this->load->view('menu_view.php', $data, true);
            //$data['sanpham'] = $this->load->view('sanpham_view.php', null, true);
            $data['sanpham'] = $this->load->view('gioithieu_view.php', null, true);
            $data['sps'] = $this->sanpham_model->sanphammoi();
            $data['new'] = $this->load->view('sanphammoi_view.php',$data, true);
            if(isset($_SESSION['mataikhoan']))
            {
                $mataikhoan = $_SESSION['mataikhoan'];
                $data['taikhoan'] = $this->taikhoan_model->laythongtintaikhoan($mataikhoan);
                $data['dangnhap'] = $this->load->view('taikhoan_view.php', $data, true);
                $this->load->view('trangchu_view.php', $data);
            }
            else
            {
                $data['dangnhap'] = $this->load->view('dangnhap_view', null, true);
    			$this->load->view('trangchu_view.php', $data);
            }
		}
        function hienthisanpham()
        {
            $masp = $this->input->post('id');
            if($masp!= 1)
            {
                $data['sanphams'] = $this->sanpham_model->laysanphamtheomasp($masp);
                echo $this->load->view('sanpham_view.php', $data, true);
            }
            else
            {
                echo $this->load->view('gioithieu_view.php');
            }
        }
        function chitietsanpham()
        {
            $masp = $this->input->post('masp');
            $data['chitiets'] = $this->sanpham_model->laythongtinsanphamtheomasp($masp);
            echo $this->load->view('chitietsanpham_view.php', $data);
        }
        function kiemtradanhgia()
        {
            $masp = $this->input->post('masp');
            $mataikhoan = $_SESSION['mataikhoan'];
            echo $this->sanpham_model->kiemtradanhgia($mataikhoan, $masp);
        }
        function danhgiasanpham()
        {
            $masp = $this->input->post('masp');
            $mataikhoan = $_SESSION['mataikhoan'];
            $uytin = intval($this->input->post('uytin'));
            $chatluong = intval($this->input->post('chatluong'));
            $gia = intval($this->input->post('gia'));
            $this->sanpham_model->danhgiasanpham($masp, $mataikhoan,$uytin, $chatluong, $gia);
        }
        
        function timkiemsanpham()
        {
            $timkiem = $this->input->post('timkiem');
            $data['sanphams'] = $this->sanpham_model->timsanpham($timkiem);
            if(count($data['sanphams']) == 0)
                echo 'Không tìm thấy sản phẩm như yêu cầu!!!!';
            else
                echo $this->load->view('sanpham_view.php', $data); 
        }
	}
	